import java.io.*;

public class Test {
    /**
     * OutputStream概述
     *      方法
     */
    public static void main3(String[] args) {
        /**
         * OutputStream方法
         *      write(int b),将字节写入
         *      write(byte[] b),将全部字节写入
         *      write(byte[] b,int off,int len),从off位置开始将len个字节全部写入
         *      close(),关闭字节流
         *      flush(),刷新,防止数据遗留在缓冲区
         * 注：
         *      打开文件会清空文件,创建实例时,可以选择是否追加
         */
        try (OutputStream outputStream = new FileOutputStream("D:/text", true)) {//(文件位置,是否追加)
            outputStream.write(97);
            outputStream.write(98);
            outputStream.write(99);
            outputStream.write(100);
        } catch (FileNotFoundException e) {
            throw new RuntimeException(e);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }

    }
    /**
     * scanner字符读取，使用charset读取is字符集
     * Scanner scanner = new Scanner(InputStream is,String charset)
     */

    /**
     * 字符流读取
     * read(),通过系统api访问硬盘,低效
     * read(byte),把数据填充到数组中
     */
    public static void main2(String[] args) throws IOException {
        try (InputStream is = new FileInputStream("D:/text.txt")) {
            byte[] buf = new byte[1024];
            int len;

            while (true) {
                        len = is.read(buf);
                if (len == -1) {
                    // 代表⽂件已经全部读完
                    break;
                }

                for (int i = 0; i < len; i++) {
                    System.out.printf("%c", buf[i]);
                }
            }
        }
    }
        public static void main1(String[] args) throws IOException {
            try (InputStream is = new FileInputStream("D:/text.txt")) {
                while (true) {
                    int b = is.read();
                    if (b == -1) {
                        // 代表⽂件已经全部读完
                        break;
                    }
                    System.out.printf("%c", b);
                }
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
    }
}
